package restlet.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity{
	// Declare our Views, so we can access them later
	private EditText username;
	private EditText password;
	private Button loginButton;
	private Button cancelButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.username_password);
        
        // Get the EditText and Button References
        username = (EditText)findViewById(R.id.username_edit);
        password = (EditText)findViewById(R.id.password_edit);
        loginButton = (Button)findViewById(R.id.LoginOkButton);
        cancelButton = (Button)findViewById(R.id.LoginCancelButton);
        
        // Set Click Listener
        loginButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		// Check Login
        		String mUsername = username.getText().toString();
        		String mPassword = password.getText().toString();

        		if(mUsername.equals("grocerwe1") || mUsername.equals("grocerwe2") ||
        				mUsername.equals("grocerwe3") || mUsername.equals("grocerwe4") || 
        				mUsername.equals("grocerwe5")){
        			
        			// Create the credentials object here which will be accessed 
        			// by subsequent activities
        			UserCredentials credentials = new UserCredentials (mUsername, mPassword);
        			
	                Intent i = new Intent();
	                i.setClassName("restlet.android",
	                		"restlet.android.BuildListActivity");
	                Bundle b = new Bundle();
	                b.putString("username", mUsername);
	                b.putString("password", mPassword);
	                i.putExtras(b);
	                startActivity(i);
	                finish();
        		} else {
        			Toast.makeText(getBaseContext(), 
            				"Please Enter Valid Username and Password", 
            				Toast.LENGTH_SHORT).show();
        		}
        	}
        });
        
        cancelButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		// Close the application
        		finish();
        	}
        });
    }
}